/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tuplestoreclient;

import core.Result;
import core.Callback;
import core.FileCallBack;
import core.ProtocolSD;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Jorge
 */
public class Main {



    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException, Exception {
        final TupleStoreClient client = new TupleStoreClient();

        final List<Integer> integers = new ArrayList<Integer>();
        integers.add(2);
       





        final List<FileChannel> map = new ArrayList<FileChannel>();

        for (Integer integer : integers) {
            RandomAccessFile accessFile = new RandomAccessFile("c:\\teste\\teste.txt", "r");
            map.add(accessFile.getChannel());
        }



        client.put(integers, map, new Callback<Integer, Boolean>() {

           

            public void onError(Exception e) {
                System.out.println(e);
            }

            public void onResult(Result<Integer, Boolean> v) {
                System.out.println("fim de put " + v.getMap().keySet());
                                
                
                callRead(client, integers);

            }
        });

       

        
//        client.getLocationToWrite(integers, integers ,new Callback<Integer, Url>() {
//
//            public void onError(Exception e) {
//
//            }
//
//            public void onResult(Result<Integer, Url> v) {
//               if(v.getMap().size()!=0){
//                Map<Integer,FileChannel> map1 = new HashMap<Integer, FileChannel>();
//               for(Integer integer : v.getMap().keySet()){
//                    map1.put(integer, map.get(integer));
//               }
//
//               client.putData(v.getMap(), map1,new Callback<Integer, Boolean>() {
//
//                    public void onError(Exception e) {
//                        System.out.println(e);
//                    }
//
//                    public void onResult(Result<Integer, Boolean> v) {
//                        System.out.println("resultado "+new LinkedList(v.getMap().keySet()));
//                        callRead(client, integers);
//                    }
//                });
//               }else{
//
//               }
//            }
//        });







//}

    }

    public static void callRead(final TupleStoreClient client, List<Integer> integers) {

        client.get(integers, new FileCallBack() {

            ByteBuffer buffer = ByteBuffer.allocate(1024);

            @Override
            public void onReadParcialParam(byte[] readedBytes, int tmpParcial, int remaning) {

                buffer.put(readedBytes);

            }

            @Override
            public void onResult() {
                System.out.println("fim de leitura");
            }

            @Override
            public void onNewFile(int key, int size) {
                buffer.clear();
                
            }

            @Override
            public void onFinishReadFile() {
                buffer.flip();
                System.out.println(new String(ProtocolSD.getBytes(buffer, 0, buffer.limit())));
            }

            public void onError(Exception e) {
                System.out.println("erro " + e.getMessage());
            }
        });

    }
}
